<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Manipulating Measurements - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.measure.edit.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.measure.edit.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.measure.output.html">Outputting measurements</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.measure.html">Zend_Measure</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.measure.types.html">Types of measurements</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.measure.edit" class="section"><div class="info"><h1 class="title">Manipulating Measurements</h1></div>
    

    <p class="para">
        Parsing and normalization of input, combined with output to localized notations makes data
        accessible to users in different locales. Many additional methods exist in
        <span class="classname">Zend_Measure_*</span> components to manipulate and work with this data,
        after it has been normalized.
    </p>

    <ul class="itemizedlist">
        <li class="listitem">
            <p class="para">
                <a href="zend.measure.edit.html#zend.measure.edit.convert" class="link">Convert</a>
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <a href="zend.measure.edit.html#zend.measure.edit.add" class="link">Add and subtract</a>
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <a href="zend.measure.edit.html#zend.measure.edit.equal" class="link">Compare to boolean</a>
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <a href="zend.measure.edit.html#zend.measure.edit.compare" class="link">Compare to greater/smaller</a>
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <a href="zend.measure.edit.html#zend.measure.edit.changevalue" class="link">Manually change values</a>
            </p>
        </li>

        <li class="listitem">
            <p class="para">
                <a href="zend.measure.edit.html#zend.measure.edit.changetype" class="link">Manually change types</a>
            </p>
        </li>
    </ul>

    <div class="section" id="zend.measure.edit.convert"><div class="info"><h1 class="title">Convert</h1></div>
        

        <p class="para">
            Probably the most important feature is the conversion into different units of
            measurement. The conversion of a unit can be done any number of times using the method
             <span class="methodname">convertTo()</span>. Units of measurement can only be converted to
            other units of the same type (class). Therefore, it is not possible to convert (e.g.) a
            length into a weight, which would might encourage poor programming practices and allow
            errors to propagate without exceptions.
        </p>

        <p class="para">
            The  <span class="methodname">convertTo()</span> method accepts an optional parameter. With
            this parameter you can define an precision for the returned output. The standard
            precision is &#039;2&#039;.
        </p>

        <div class="example" id="zend.measure.edit.convert.example-1"><div class="info"><p><b>Example #1 Convert</b></p></div>
            

            <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de&#039;);
$mystring = &quot;1.234.567,89&quot;;
$unit = new Zend_Measure_Weight($mystring,&#039;POND&#039;, $locale);

print &quot;Kilo:&quot;.$unit-&gt;convertTo(&#039;KILOGRAM&#039;);

// constants are considered &quot;better practice&quot; than strings
print &quot;Ton:&quot;.$unit-&gt;convertTo(Zend_Measure_Weight::TON);

// define a precision for the output
print &quot;Ton:&quot;.$unit-&gt;convertTo(Zend_Measure_Weight::TON, 3);
</pre>

        </div>
    </div>

    <div class="section" id="zend.measure.edit.add"><div class="info"><h1 class="title">Add and subtract</h1></div>
        

        <p class="para">
            Measurements can be added together using  <span class="methodname">add()</span> and subtracted
            using  <span class="methodname">sub()</span>. The result will use the same type as the
            originating object. Dynamic objects support a fluid style of programming, where complex
            sequences of operations can be nested without risk of side-effects altering the input
            objects.
        </p>

        <p class="para">
            <div class="example" id="zend.measure.edit.add.example-1"><div class="info"><p><b>Example #2 Adding units</b></p></div>
                

                <pre class="programlisting brush: php">
// Define objects
$unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);

// Add $unit2 to $unit
$sum = $unit-&gt;add($unit2);

echo $sum; // outputs &quot;300 cm&quot;
</pre>

            </div>
        </p>

        <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Automatic conversion</b><br /></span>
            

            <p class="para">
                Adding one object to another will automatically convert it to the correct unit. It
                is not necessary to call <a href="zend.measure.edit.html#zend.measure.edit.convert" class="link"> <span class="methodname">convertTo()</span></a>
                before adding different units.
            </p>
        </p></blockquote>

        <p class="para">
            <div class="example" id="zend.measure.edit.add.example-2"><div class="info"><p><b>Example #3 Subtract</b></p></div>
                

                <div class="example-contents"><p>
                    Subtraction of measurements works just like addition.
                </p></div>

                <pre class="programlisting brush: php">
// Define objects
$unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);

// Subtract $unit2 from $unit
$sum = $unit-&gt;sub($unit2);

echo $sum;
</pre>

            </div>
        </p>
    </div>

    <div class="section" id="zend.measure.edit.equal"><div class="info"><h1 class="title">Compare</h1></div>
        

        <p class="para">
            Measurements can also be compared, but without automatic unit conversion. Thus,
             <span class="methodname">equals()</span> returns <b><tt>TRUE</tt></b>, only if both the
            value and the unit of measure are identical.
        </p>

        <p class="para">
            <div class="example" id="zend.measure.edit.equal.example-1"><div class="info"><p><b>Example #4 Different measurements</b></p></div>
                

                <pre class="programlisting brush: php">
// Define measurements
$unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);

if ($unit-&gt;equals($unit2)) {
    print &quot;Both measurements are identical&quot;;
} else {
    print &quot;These are different measurements&quot;;
}
</pre>

            </div>

            <div class="example" id="zend.measure.edit.equal.example-2"><div class="info"><p><b>Example #5 Identical measurements</b></p></div>
                

                <pre class="programlisting brush: php">
// Define measurements
$unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);

$unit2-&gt;setType(Zend_Measure_Length::CENTIMETER);

if ($unit-&gt;equals($unit2)) {
    print &quot;Both measurements are identical&quot;;
} else {
    print &quot;These are different measurements&quot;;
}
</pre>

            </div>
        </p>
    </div>

    <div class="section" id="zend.measure.edit.compare"><div class="info"><h1 class="title">Compare</h1></div>
        

        <p class="para">
            To determine if a measurement is less than or greater than another, use
             <span class="methodname">compare()</span>, which returns 0, -1 or 1 depending on the difference
            between the two objects. Identical measurements will return 0. Lesser ones will return a
            negative, greater ones a positive value.
        </p>

        <p class="para">
            <div class="example" id="zend.measure.edit.compare.example-1"><div class="info"><p><b>Example #6 Difference</b></p></div>
                

                <pre class="programlisting brush: php">
$unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
$unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
$unit3 = new Zend_Measure_Length(1.2, Zend_Measure_Length::METER);

print &quot;Equal:&quot;.$unit2-&gt;compare($unit);
print &quot;Lesser:&quot;.$unit2-&gt;compare($unit3);
print &quot;Greater:&quot;.$unit3-&gt;compare($unit2);
</pre>

            </div>
        </p>
    </div>

    <div class="section" id="zend.measure.edit.changevalue"><div class="info"><h1 class="title">Manually change values</h1></div>
        

        <p class="para">
            To change the value of a measurement explicitly, use
             <span class="methodname">setValue()</span>. to overwrite the current value. The parameters are
            the same as the constructor.
        </p>

        <p class="para">
            <div class="example" id="zend.measure.edit.changevalue.example-1"><div class="info"><p><b>Example #7 Changing a value</b></p></div>
                

                <pre class="programlisting brush: php">
$locale = new Zend_Locale(&#039;de_AT&#039;);
$unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);

$unit-&gt;setValue(1.2);
echo $unit;

$unit-&gt;setValue(1.2, Zend_Measure_Length::KILOMETER);
echo $unit;

$unit-&gt;setValue(&quot;1.234,56&quot;, Zend_Measure_Length::MILLIMETER,$locale);
echo $unit;
</pre>

            </div>
        </p>
    </div>

    <div class="section" id="zend.measure.edit.changetype"><div class="info"><h1 class="title">Manually change types</h1></div>
        

        <p class="para">
            To change the type of a measurement without altering its value use
             <span class="methodname">setType()</span>.
        </p>

        <div class="example" id="zend.measure.edit.changetype.example-1"><div class="info"><p><b>Example #8 Changing the type</b></p></div>
            

            <pre class="programlisting brush: php">
$unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
echo $unit; // outputs &quot;1 m&quot;

$unit-&gt;setType(Zend_Measure_Length::KILOMETER);
echo $unit; // outputs &quot;1000 km&quot;
</pre>

        </div>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.measure.output.html">Outputting measurements</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.measure.html">Zend_Measure</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.measure.types.html">Types of measurements</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.measure.html">Zend_Measure</a></li>
  <li><a href="zend.measure.introduction.html">Introduction</a></li>
  <li><a href="zend.measure.creation.html">Creation of Measurements</a></li>
  <li><a href="zend.measure.output.html">Outputting measurements</a></li>
  <li class="active"><a href="zend.measure.edit.html">Manipulating Measurements</a></li>
  <li><a href="zend.measure.types.html">Types of measurements</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>